package com.competition.commgr.service.impl;

import com.competition.commgr.domain.Competitionitem;
import com.competition.commgr.domain.Competitions;
import com.competition.commgr.domain.vo.CompetitionItemVo;
import com.competition.commgr.mapper.CompetitionitemMapper;
import com.competition.commgr.service.ICompetitionitemService;
import com.competition.commgr.service.ICompetitionsService;
import com.competition.common.exception.ServiceException;
import com.competition.common.utils.StringUtils;
import com.competition.common.utils.bean.BeanValidators;
import com.competition.system.service.ISysDictDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 赛项管理Service业务层处理
 * 
 * @author cui
 * @date 2025-01-27
 */
@Service
public class CompetitionitemServiceImpl implements ICompetitionitemService 
{
    private static final Logger log = LoggerFactory.getLogger(CompetitionitemServiceImpl.class);
    @Autowired
    protected Validator validator;
    @Autowired
    private CompetitionitemMapper competitionitemMapper;
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ICompetitionsService competitionsService;

    /**
     * 查询赛项管理
     * 
     * @param comItemId 赛项管理主键
     * @return 赛项管理
     */
    @Override
    public Competitionitem selectCompetitionitemByComItemId(Long comItemId)
    {
        return competitionitemMapper.selectCompetitionitemByComItemId(comItemId);
    }

    /**
     * 查询赛项管理列表
     * 
     * @param competitionitem 赛项管理
     * @return 赛项管理
     */
    @Override
    public List<Competitionitem> selectCompetitionitemList(Competitionitem competitionitem)
    {
        return competitionitemMapper.selectCompetitionitemList(competitionitem);
    }

    /**
     * 新增赛项管理
     * 
     * @param competitionitem 赛项管理
     * @return 结果
     */
    @Override
    public int insertCompetitionitem(Competitionitem competitionitem)
    {
        return competitionitemMapper.insertCompetitionitem(competitionitem);
    }

    /**
     * 修改赛项管理
     * 
     * @param competitionitem 赛项管理
     * @return 结果
     */
    @Override
    public int updateCompetitionitem(Competitionitem competitionitem)
    {
        return competitionitemMapper.updateCompetitionitem(competitionitem);
    }

    /**
     * 批量删除赛项管理
     * 
     * @param comItemIds 需要删除的赛项管理主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionitemByComItemIds(Long[] comItemIds)
    {
        return competitionitemMapper.deleteCompetitionitemByComItemIds(comItemIds);
    }

    /**
     * 删除赛项管理信息
     * 
     * @param comItemId 赛项管理主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionitemByComItemId(Long comItemId)
    {
        return competitionitemMapper.deleteCompetitionitemByComItemId(comItemId);
    }

    @Override
    public List<CompetitionItemVo> selectCompetitionitemVoList(Competitionitem competitionitem) {
        return competitionitemMapper.selectCompetitionitemVoList(competitionitem);
    }

    @Override
    public String importCompetitionitem(List<CompetitionItemVo> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入赛事类别数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (CompetitionItemVo competitionItemVo : list)
        {
            try
            {
                // 验证是否存在这个赛项
                CompetitionItemVo  competitionItemVo1= competitionitemMapper.selectOneCompetitionitem(competitionItemVo);
                if (competitionItemVo1 == null)
                {
                    BeanValidators.validateWithException(validator, competitionItemVo);
                    //验证赛事名称是否正确
                    Competitions competitions = competitionsService.selectCompetitionsByCompetitionName(competitionItemVo.getCompetitionName());
                    if (competitions==null)
                    {
                        failureNum++;
                        String msg = "<br/>" + failureNum +  " 导入失败：赛事名称错误";
                        failureMsg.append(msg);
                        continue;
                    }else {
                        competitionItemVo.setCompetitionId(competitions.getCompetitionId());
                    }

                    competitionItemVo.setCreateBy(operName);
                    competitionitemMapper.insertCompetitionitem(competitionItemVo);
                    successNum++;
                }
                else if (updateSupport)
                {
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum +  " 导入失败：" + competitionItemVo.getComItemName() + "已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum +  " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if(failureNum == 0){
            return successMsg.toString();
        }else {
            return failureMsg.toString();
        }
    }

    @Override
    public Competitionitem selectCompetitionitemByName(String comItemName) {
        return competitionitemMapper.selectCompetitionitemByName(comItemName);
    }
}
